package org.generic.test01;

import java.awt.*;
import java.util.ArrayList;

/**
 * @author CSY
 * @date 2025 - 03 - 16
 */
public class Generic {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Dog("小白",5));
        arrayList.add(new Dog("小红",2));
        arrayList.add(new Dog("小黑",3));

        //假设不小心添加了一只猫该如何？ - 报错（类型转换异常）
        arrayList.add(new Cat("小橘",5));

        //由于在使用ArrayList集合时没有使用泛型，那么默认的类型就是Object类型，在赋值给其他类型时需要强转
        for (Object o : arrayList) {
            Dog d = (Dog)o;
            System.out.println(d.getName()+"-"+d.getAge());
        }
    }
}
class Dog{
    private String name;
    private int age;

    public Dog(){}

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Cat{
    private String name;
    private int age;
    public Cat(){}
    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
